// ../api/Knowledge.js
import request from '../utils/request.js';

/**
 * 1. 获取系统知识库列表
 */
export const getSystemKnowledgeBasesService = async () => {
  const result = await request.get('/api/knowledge/system');
  // 统一响应格式校验
  if (result.code !== 1) {
    throw new Error(result.message || '获取系统知识库失败');
  }
  return result.data;
};

/**
 * 2. 获取用户个人知识库列表
 * @param {string} email - 用户邮箱
 */
export const getUserKnowledgeBasesService = async (email) => {
  const result = await request.get('/api/knowledge/user', {
    params: { email } // 拼接查询参数
  });
  if (result.code !== 1) {
    throw new Error(result.message || '获取个人知识库失败');
  }
  return result.data;
};

/**
 * 3. 创建知识库（含多文件上传）
 * @param {FormData} formData - 表单数据（name/description/email/files）
 */
export const createKnowledgeBaseService = async (formData, onUploadProgress) => {
  const result = await request.post('/api/knowledge/create', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress // 上传进度回调（用于进度条）
  });
  if (result.code !== 1) {
    throw new Error(result.message || '创建知识库失败');
  }
  return result.data;
};

/**
 * 4. 上传文件到已有知识库
 * @param {FormData} formData - 表单数据（kbId/email/file）
 * @param {Function} onUploadProgress - 进度回调
 */
export const uploadFileToKbService = async (formData, onUploadProgress) => {
  const result = await request.post('/api/knowledge/upload-file', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress
  });
  if (result.code !== 1) {
    throw new Error(result.message || '文件上传失败');
  }
  return result.data;
};

/**
 * 5. 获取知识库下的文件列表
 * @param {string} kbId - 知识库ID
 */
export const getKbFilesService = async (kbId) => {
  const result = await request.get('/api/knowledge/files', {
    params: { kbId }
  });
  if (result.code !== 1) {
    throw new Error(result.message || '获取文件列表失败');
  }
  return result.data;
};

/**
 * 6. 删除知识库中的文件
 * @param {Object} params - { kbId, fileId, email }
 */
export const deleteKbFileService = async (params) => {
  const result = await request.delete('/api/knowledge/delete-file', {
    data: params // delete请求传body（适配后端接收方式）
  });
  if (result.code !== 1) {
    throw new Error(result.message || '删除文件失败');
  }
  return result.data;
};

/**
 * 7. 删除个人知识库
 * @param {Object} params - { kbId, email }
 */
export const deleteKnowledgeBaseService = async (params) => {
  const result = await request.delete('/api/knowledge/delete', {
    data: params
  });
  if (result.code !== 1) {
    throw new Error(result.message || '删除知识库失败');
  }
  return result.data;
};

/**
 * 8. 选择知识库关联机器人
 * @param {Object} params - { robotId, knowledgeBaseId, isSystem }
 */
export const selectKbService = async (params) => {
  const result = await request.post('/api/knowledge/select', params);
  if (result.code !== 1) {
    throw new Error(result.message || '选择知识库失败');
  }
  return result.data;
};

/**
 * 9. 取消选择知识库
 * @param {Object} params - { robotId }
 */
export const deselectKbService = async (params) => {
  const result = await request.post('/api/knowledge/deselect', params);
  if (result.code !== 1) {
    throw new Error(result.message || '取消选择知识库失败');
  }
  return result.data;
};

/**
 * 10. 获取机器人当前选中的知识库
 * @param {number} robotId - 机器人ID
 */
export const getSelectedKbService = async (robotId) => {
  const result = await request.get('/api/knowledge/selected', {
    params: { robotId }
  });
  if (result.code !== 1) {
    throw new Error(result.message || '获取已选知识库失败');
  }
  return result.data;
};

/**
 * 创建系统知识库（仅管理员）
 */
export const createSystemKnowledgeBaseService = async (formData, onUploadProgress) => {
  const result = await request.post('/api/knowledge/system/create', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress
  });
  if (result.code !== 1) throw new Error(result.message || '创建系统知识库失败');
  return result.data;
};

/**
 * 上传文件到系统知识库（仅管理员）
 */
export const uploadFileToSystemKbService = async (formData, onUploadProgress) => {
  const result = await request.post('/api/knowledge/system/upload-file', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
    onUploadProgress
  });
  if (result.code !== 1) throw new Error(result.message || '系统知识库文件上传失败');
  return result.data;
};

/**
 * 删除系统知识库（仅管理员）
 */
export const deleteSystemKnowledgeBaseService = async (params) => {
  const result = await request.delete('/api/knowledge/system/delete', { data: params });
  if (result.code !== 1) throw new Error(result.message || '删除系统知识库失败');
  return result.data;
};

/**
 * 删除系统知识库文件（仅管理员）
 */
export const deleteSystemKbFileService = async (params) => {
  const result = await request.delete('/api/knowledge/system/delete-file', { data: params });
  if (result.code !== 1) throw new Error(result.message || '删除系统知识库文件失败');
  return result.data;
};

/**
 * 获取系统知识库文件列表（仅管理员）
 */
export const getFilesBySystemKbIdService = async (systemKbId,email) => {
  const result = await request.get('/api/knowledge/system/files', { params: { systemKbId,email} });
  if (result.code !== 1) throw new Error(result.message || '获取系统知识库文件列表失败');
  return result.data;
};